﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace YArchitech.HVAC
{
	public class HeatingSystemCalc
	{
		public void DesignCalc(BuildingSystem buildingSystem, HYHeatingSystemOption option)
		{
			this.CalcPipeDiameter(ref buildingSystem, option);
			this.CalcHYPipeResistance(ref buildingSystem, option);
			this.CalcTCValvePress(ref buildingSystem);
			this.CalcUnbalanceRate(ref buildingSystem);
		}

		public void CheckedCalc(BuildingSystem buildingSystem, HYHeatingSystemOption option)
		{
			this.CalcHYPipeResistance(ref buildingSystem, option);
			this.CalcTCValvePress(ref buildingSystem);
			this.CalcUnbalanceRate(ref buildingSystem);
		}

		private void CalcTCValvePress(ref BuildingSystem buildingSystem)
		{
			double num = 5000.0;
			double num2 = this.CalcMaxPressAcutal(buildingSystem);
			num2 += num;
			Log.Info(new string[]
			{
				string.Format("totalAvailablePress is {0}", num2)
			});
			for (int i = 0; i < buildingSystem.RisePipes.Count; i++)
			{
				double num3 = num2;
				num3 -= buildingSystem.SupplyPipes.SumResistance(i + 1);
				if (buildingSystem.ConnectType == 0)
				{
					num3 -= buildingSystem.BackPipes.SumResistance(buildingSystem.RisePipes.Count - i);
				}
				else
				{
					num3 -= buildingSystem.BackPipes.SumResistance(i + 1);
				}
				RisePipeSystem at = buildingSystem.GetAt(i + 1);
				for (int j = 0; j < at.FloorSystems.Count; j++)
				{
					FloorSystem at2 = at.GetAt(j + 1);
					double num4 = num3;
					num4 -= at.SupplyPipes.SumResistance(j + 1);
					if (at.ConnectType == 0)
					{
						num4 -= at.BackPipes.SumResistance(at.FloorSystems.Count - j);
					}
					else
					{
						num4 -= at.BackPipes.SumResistance(j + 1);
					}
					num4 -= at2.GravityPress;
					if (at2.SupplyPipe != null && at2.BackPipe != null)
					{
						num4 -= at2.SupplyPipe.Resistance;
						num4 -= at2.BackPipe.Resistance;
					}
					for (int k = 0; k < at2.InDoorSystem.Count; k++)
					{
						double num5 = num4;
						IndoorSystem at3 = at2.GetAt(k + 1);
						at3.AvailPress = num5;
						if (at3.Loops.Count > 1)
						{
							num5 -= at3.SupplyPipe.Resistance;
							num5 -= at3.BackPipe.Resistance;
						}
						for (int l = 0; l < at3.Loops.Count; l++)
						{
							double num6 = num5;
							IndoorLoop at4 = at3.GetAt(l + 1);
							for (int m = 0; m < at4.EquipmentLoops.Count; m++)
							{
								double num7 = num6;
								EquipmentLoop at5 = at4.GetAt(m + 1);
								num7 -= at4.SupplyPipes.SumResistance(m + 1);
								if (at4.ConnType == 0)
								{
									num7 -= at4.BackPipes.SumResistance(at4.EquipmentLoops.Count - m);
								}
								else
								{
									num7 -= at4.BackPipes.SumResistance(m + 1);
								}
								num7 -= at5.SupplyPipeSet.SumResistance();
								num7 -= at5.BackPipeSet.SumResistance();
								at5.ResistTcValve = num7;
							}
						}
					}
				}
			}
		}

		private void CalcUnbalanceRate(ref BuildingSystem buildingSystem)
		{
			buildingSystem.CalcUnbalance();
		}

		private void CalcPipeDiameter(ref BuildingSystem buildingSystem, HYHeatingSystemOption option)
		{
			this.CalcPipeDiameter(buildingSystem.SupplyPipes, option, 0);
			this.CalcPipeDiameter(buildingSystem.BackPipes, option, 0);
			foreach (RisePipeSystem risePipeSystem in buildingSystem.RisePipes)
			{
				this.CalcPipeDiameter(risePipeSystem.SupplyPipes, option, 0);
				this.CalcPipeDiameter(risePipeSystem.BackPipes, option, 0);
				foreach (FloorSystem floorSystem in risePipeSystem.FloorSystems)
				{
					if (floorSystem.SupplyPipe != null)
					{
						this.CalcPipeDiameter(floorSystem.SupplyPipe, option, 0);
						this.CalcPipeDiameter(floorSystem.BackPipe, option, 0);
					}
					foreach (IndoorSystem indoorSystem in floorSystem.InDoorSystem)
					{
						if (indoorSystem.SupplyPipe != null)
						{
							this.CalcPipeDiameter(indoorSystem.SupplyPipe, option, 0);
							this.CalcPipeDiameter(indoorSystem.BackPipe, option, 0);
						}
						foreach (IndoorLoop indoorLoop in indoorSystem.Loops)
						{
							this.CalcPipeDiameter(indoorLoop.SupplyPipes, option, 0);
							this.CalcPipeDiameter(indoorLoop.BackPipes, option, 0);
							foreach (EquipmentLoop equipmentLoop in indoorLoop.EquipmentLoops)
							{
								this.CalcPipeDiameter(equipmentLoop.SupplyPipeSet, option, equipmentLoop.Diameter);
								this.CalcPipeDiameter(equipmentLoop.BackPipeSet, option, equipmentLoop.Diameter);
							}
						}
					}
				}
			}
		}

		private void CalcPipeDiameter(PipeSet pipes, HYHeatingSystemOption option, int diameter = 0)
		{
			pipes.ForEach(delegate(HyCalcPipe pipe)
			{
				this.CalcPipeDiameter(pipe, option, diameter);
			});
		}

		private void CalcPipeDiameter(HyCalcPipe pipe, HYHeatingSystemOption option, int diameter = 0)
		{
			foreach (ValveAndFitting valveAndFitting in pipe.Valves)
			{
				valveAndFitting.GetResCoefLocal(pipe.DN);
			}
			foreach (PipeLine pipeLine in pipe.PipesLines)
			{
				pipeLine.CalcPipeDiameter(option, diameter);
			}
		}

		public void CalcHYPipeResistance(ref BuildingSystem buildingSystem, HYHeatingSystemOption option)
		{
			this.CalcHYPipeResistance(buildingSystem.SupplyPipes, option);
			this.CalcHYPipeResistance(buildingSystem.BackPipes, option);
			foreach (RisePipeSystem risePipeSystem in buildingSystem.RisePipes)
			{
				this.CalcHYPipeResistance(risePipeSystem.SupplyPipes, option);
				this.CalcHYPipeResistance(risePipeSystem.BackPipes, option);
				foreach (FloorSystem floorSystem in risePipeSystem.FloorSystems)
				{
					if (floorSystem.SupplyPipe != null)
					{
						this.CalcHYPipeResistance(floorSystem.SupplyPipe, option);
						this.CalcHYPipeResistance(floorSystem.BackPipe, option);
					}
					foreach (IndoorSystem indoorSystem in floorSystem.InDoorSystem)
					{
						if (indoorSystem.SupplyPipe != null)
						{
							this.CalcHYPipeResistance(indoorSystem.SupplyPipe, option);
							this.CalcHYPipeResistance(indoorSystem.BackPipe, option);
						}
						foreach (IndoorLoop indoorLoop in indoorSystem.Loops)
						{
							this.CalcHYPipeResistance(indoorLoop.SupplyPipes, option);
							this.CalcHYPipeResistance(indoorLoop.BackPipes, option);
							foreach (EquipmentLoop equipmentLoop in indoorLoop.EquipmentLoops)
							{
								this.CalcHYPipeResistance(equipmentLoop.SupplyPipeSet, option);
								this.CalcHYPipeResistance(equipmentLoop.BackPipeSet, option);
							}
						}
					}
				}
			}
		}

		private void CalcHYPipeResistance(PipeSet pSet, HYHeatingSystemOption option)
		{
			pSet.ForEach(delegate(HyCalcPipe pipe)
			{
				this.CalcHYPipeResistance(pipe, option);
			});
		}

		private void CalcHYPipeResistance(HyCalcPipe pipe, HYHeatingSystemOption option)
		{
			pipe.PipesLines.ForEach(delegate(PipeLine p)
			{
				p.CalcResistance((double)option.SupplyTemperature, (double)option.ReturnTemperature);
			});
		}

		public double CalcMaxPressAcutal(BuildingSystem buildingSystem)
		{
			buildingSystem.CalcRiserPressAcutal();
			this.index_riser = buildingSystem.GetWorstRisesystemIndex();
			RisePipeSystem at = buildingSystem.GetAt(this.index_riser);
			this.index_floor = at.WorstIndex;
			FloorSystem at2 = at.GetAt(this.index_floor);
			this.index_indoor = at2.WorstIndex;
			IndoorSystem at3 = at2.GetAt(this.index_indoor);
			this.index_loop = at3.WorstIndex;
			IndoorLoop at4 = at3.GetAt(this.index_loop);
			this.index_eq = at4.WorstIndex;
			buildingSystem.WorstCircleInfo = string.Format("最不利环路：共用立管[{0}]楼层[{1}]户内[{2}]环路[{3}]散热器[{4}]所在的环路。", new object[]
			{
				this.index_riser,
				this.index_floor,
				this.index_indoor,
				this.index_loop,
				this.index_eq
			});
			return at.PressAcutal;
		}

		public void SetPipeTemp(BuildingSystem buildingSystem, HYHeatingSystemOption option)
		{
			this.SetPipeTemp(buildingSystem.SupplyPipes, (double)option.SupplyTemperature);
			this.SetPipeTemp(buildingSystem.BackPipes, (double)option.ReturnTemperature);
            //Action<PipeLine> <>9__0;
            //Action<PipeLine> <>9__1;
            //Action<PipeLine> <>9__2;
            //Action<PipeLine> <>9__3;
            //foreach (RisePipeSystem risePipeSystem in buildingSystem.RisePipes)
            //{
            //    this.SetPipeTemp(risePipeSystem.SupplyPipes, (double)option.SupplyTemperature);
            //    this.SetPipeTemp(risePipeSystem.BackPipes, (double)option.ReturnTemperature);
            //    foreach (FloorSystem floorSystem in risePipeSystem.FloorSystems)
            //    {
            //        if (floorSystem.SupplyPipe != null)
            //        {
            //            IEnumerable<PipeLine> pipesLines = floorSystem.SupplyPipe.PipesLines;
            //            Action<PipeLine> action;
            //            if ((action = <>9__0) == null)
            //            {
            //                action = (<>9__0 = delegate(PipeLine p)
            //                {
            //                    p.Temperature = (double)option.SupplyTemperature;
            //                });
            //            }
            //            pipesLines.ForEach(action);
            //            IEnumerable<PipeLine> pipesLines2 = floorSystem.BackPipe.PipesLines;
            //            Action<PipeLine> action2;
            //            if ((action2 = <>9__1) == null)
            //            {
            //                action2 = (<>9__1 = delegate(PipeLine p)
            //                {
            //                    p.Temperature = (double)option.SupplyTemperature;
            //                });
            //            }
            //            pipesLines2.ForEach(action2);
            //        }
            //        foreach (IndoorSystem indoorSystem in floorSystem.InDoorSystem)
            //        {
            //            if (indoorSystem.SupplyPipe != null)
            //            {
            //                IEnumerable<PipeLine> pipesLines3 = indoorSystem.SupplyPipe.PipesLines;
            //                Action<PipeLine> action3;
            //                if ((action3 = <>9__2) == null)
            //                {
            //                    action3 = (<>9__2 = delegate(PipeLine p)
            //                    {
            //                        p.Temperature = (double)option.SupplyTemperature;
            //                    });
            //                }
            //                pipesLines3.ForEach(action3);
            //                IEnumerable<PipeLine> pipesLines4 = indoorSystem.BackPipe.PipesLines;
            //                Action<PipeLine> action4;
            //                if ((action4 = <>9__3) == null)
            //                {
            //                    action4 = (<>9__3 = delegate(PipeLine p)
            //                    {
            //                        p.Temperature = (double)option.ReturnTemperature;
            //                    });
            //                }
            //                pipesLines4.ForEach(action4);
            //            }
            //            foreach (IndoorLoop indoorLoop in indoorSystem.Loops)
            //            {
            //                this.SetPipeTemp(indoorLoop.SupplyPipes, (double)option.SupplyTemperature);
            //                this.SetPipeTemp(indoorLoop.BackPipes, (double)option.ReturnTemperature);
            //                foreach (EquipmentLoop equipmentLoop in indoorLoop.EquipmentLoops)
            //                {
            //                    this.SetPipeTemp(equipmentLoop.SupplyPipeSet, (double)option.SupplyTemperature);
            //                    this.SetPipeTemp(equipmentLoop.BackPipeSet, (double)option.ReturnTemperature);
            //                }
            //            }
            //        }
            //    }
            //}
		}

		private void SetPipeTemp(PipeSet pSet, double dTemp)
		{
            //Action<PipeLine> <>9__1;
            //pSet.ForEach(delegate(HyCalcPipe pipe)
            //{
            //    IEnumerable<PipeLine> pipesLines = pipe.PipesLines;
            //    Action<PipeLine> action;
            //    if ((action = <>9__1) == null)
            //    {
            //        action = (<>9__1 = delegate(PipeLine p)
            //        {
            //            p.Temperature = dTemp;
            //        });
            //    }
            //    pipesLines.ForEach(action);
            //});
		}

		private int index_eq = 1;

		private int index_loop = 1;

		private int index_indoor = 1;

		private int index_floor = 1;

		private int index_riser = 1;

		private const double Roughness = 0.2;

		private const double AverageRm = 45.0;
	}
}
